/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sh.ndt.schedule.dao;

import com.sh.ndt.common.DBObjects;
import com.sh.ndt.common.dao.BaseDAOImpl;
import com.sh.ndt.common.dao.ExtractDataRows;
import com.sh.ndt.course.entity.CourseParam;
import com.sh.ndt.course.mapper.CourseParamMapper;
import com.sh.ndt.exam.entity.Exam;
import com.sh.ndt.exam.entity.ExamScheduleDetail;
import com.sh.ndt.exam.mapper.ExamScheduleMapper;
import com.sh.ndt.exam.mapper.TestHistoryMapper;
import com.sh.ndt.exception.DAOException;
import com.sh.ndt.schedule.entity.ExamSchedule;
import com.sh.ndt.user.entity.UserDashboardDetail;
import com.sh.ndt.user.entity.UsersTestHistory;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 *
 * @author Prashanth
 */
public class ScheduleDAOImpl extends JdbcDaoSupport implements ScheduleDAO {

    private static final Logger LOGGER = Logger.getLogger(ScheduleDAOImpl.class);

    private ScheduleDAOImpl(){
        LOGGER.debug("****************** ScheduleDAOImpl Initialized **");
    }   
    
    @Override
    public UserDashboardDetail addNewSchedule(ExamSchedule examSchedule) throws DAOException {
        LOGGER.debug("Inside ScheduleDAOImpl's addNewSchedule method...");
        UserDashboardDetail result = null;

        try {
            SqlParameter sqlParam[] = new SqlParameter[9];

            sqlParam[0] = new SqlParameter("in_user_id", Types.INTEGER);
            sqlParam[1] = new SqlParameter("in_course_id", Types.INTEGER);
            sqlParam[2] = new SqlParameter("in_exam_id", Types.INTEGER);
            sqlParam[3] = new SqlParameter("in_schedule_date", Types.DATE);

            sqlParam[4] = new SqlReturnResultSet("exam_list", new ExamScheduleMapper());
            sqlParam[5] = new SqlReturnResultSet("test_history_list", new TestHistoryMapper());
            sqlParam[6] = new SqlOutParameter("OUT_TEST_ATTEMPT_COUNT", Types.INTEGER);
            sqlParam[7] = new SqlOutParameter("OUT_CREDIT_POINTS", Types.INTEGER);
            sqlParam[8] = new SqlOutParameter("OUT_SP_MESSAGE", Types.VARCHAR);

            ExtractDataRows rows = new ExtractDataRows(getJdbcTemplate().getDataSource(), DBObjects.SP_ADD_EXAM_SCHEDULE, sqlParam);
            HashMap inputs = new HashMap();
            inputs.put("in_user_id", examSchedule.getUserId());
            inputs.put("in_course_id", examSchedule.getCourseId());
            inputs.put("in_exam_id", examSchedule.getCourseId()); // This has to be changed to set exam id, instead of course Id
            inputs.put("in_schedule_date", examSchedule.getScheduleDate());

            Map results = rows.executeStoredProcedure(inputs);

            UserDashboardDetail userDashboard = new UserDashboardDetail();

            if (results.get("OUT_CREDIT_POINTS") != null) {
                userDashboard.setCreditedPoints((Integer) results.get("OUT_CREDIT_POINTS"));
            }
            if (results.get("OUT_TEST_ATTEMPT_COUNT") != null) {
                userDashboard.setMockTestAttepts((Integer) results.get("OUT_TEST_ATTEMPT_COUNT"));
            }
            if (results.get("OUT_SP_MESSAGE") != null) {
                userDashboard.setErrorMessage((String)results.get("OUT_SP_MESSAGE"));
            }
            userDashboard.setExamDetail(new Exam());
            userDashboard.setUsersTestHistoryList((List<UsersTestHistory>) results.get("test_history_list"));
            userDashboard.setExamScheduleList((List<ExamScheduleDetail>) results.get("exam_list"));
            LOGGER.debug("Adding new Schedule was successful");

            result = userDashboard;
        } catch (DAOException e) {
            LOGGER.debug(e);
        }
        return result;
    }

}
